# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from lxml import etree
import time
from fake_useragent import UserAgent

"""
远程调试
cmd 运行：
    chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\webdriver\AutomationProfile"
添加参数
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
"""

"""
驱动地址：https://sites.google.com/a/chromium.org/chromedriver/downloads
        http://npm.taobao.org/mirrors/chromedriver/
WebDriver 只能在一个页面上对元素识别与定位，对于frame/iframe 表单内嵌页面上的元素无法直接定位
需要通过switch_to.frame()方法将当前定位的主体切换为frame/iframe 表单的内嵌页面中
#切换到表单
driver.switch_to.frame("x-URS-iframe")
打开新的标签页
js=f'window.open("{url}");'
driver.execute_script(js)
------Select------
# 选中这个标签，然后使用Select创建对象
selectTag = Select(driver.find_element_by_name("jumpMenu"))
# 根据索引选择
selectTag.select_by_index(1)
# 根据值选择
selectTag.select_by_value("值")
# 根据可视的文本选择
selectTag.select_by_visible_text("显示值")
# 取消选中所有选项
selectTag.deselect_all()
------Select------
------切换窗口并关闭------
# 获取当前窗口句柄
now_handle = driver.current_window_handle
# 获取所有窗口句柄
all_handles = driver.window_handles
for handle in all_handles:
    if handle != now_handle:
        driver.switch_to.window(handle)  # 切换新打开窗口
        driver.close()
# 返回主窗口
driver.switch_to.window(now_handle)
------切换窗口并关闭------
------滚动条scrollTop=0为顶部，100000为底部------
js="var q=document.documentElement.scrollTop=100000"
------滚动条------
------返回521反爬-----
driver.get 两次.第一次获取加密cookie。第二次提交解析后的cookie
------返回521反爬-----
"""


# 初始化
def init_driver(url):
    chrome_options = Options()
    # 设置chrome不加载图片，提高速度
    # chrome_options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
    # chrome_options.add_argument('--headless') # 无头模式
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('user-agent='+UserAgent().random)  # 修改请求头，伪装浏览器型号
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 设置开发模式，避免被检测
    # driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
    # 未在PATH环境变量中设置，需要设置驱动可执行路径
    driver = webdriver.Chrome(executable_path='D:\\webdriver\\chromedriver.exe', chrome_options=chrome_options)
    driver.maximize_window()  # 窗口最大化
    driver.get(url)
    # html = driver.page_source
    # print(html)
    return driver


# 获取页面url
def get_url(drive):
    # 设置等待
    wait = WebDriverWait(driver, 10)
    WebDriverWait(driver, 50).until(
        EC.presence_of_element_located((By.XPATH, '//div[@class="doc_title clearfix"]')))
    # time.sleep(3)
    root = etree.HTML(driver.page_source)
    name = root.xpath('//div[@class="doc_title clearfix"]/h3/text()')[0].strip().split(" ")[0].split("\xa0")[0]
    ys_zhicheng = root.xpath('//div[@class="doc_title clearfix"]/h3/text()')[0].strip().split(" ")[0].split("\xa0")[2]  # 医生职称
    # 是否有学术职称
    if len(root.xpath('//div[@class="doc_title clearfix"]/h3/text()')[0].strip().split(" ")) > 1:
        xs_zhicheng = root.xpath('//div[@class="doc_title clearfix"]/h3/text()')[0].strip().split(" ")[1]  # 学术职称
    hospital = root.xpath('//div[@class="fl pr"]/p/a/text()')[0].strip()  # 医院名称
    keshi = root.xpath('//div[@class="fl pr"]/p/a/text()')[1].strip()  # 科室
    hot = root.xpath('//ul[@class="doc_info_ul1"]/li[2]/p/a/span/i/text()')[0]  # 热度
    ganxiexin = root.xpath('//ul[@class="doc_info_ul1"]/li[3]/span[1]/a/text()')[0]  # 感谢信数量
    liwu = root.xpath('//ul[@class="doc_info_ul1"]/li[3]/span[2]/a/text()')[0]  # 礼物
    fwl = root.xpath('//ul[@class="space_statistics"]/li[1]/span/text()')[0]  # 总访问
    fwl_z = root.xpath('//ul[@class="space_statistics"]/li[2]/span/text()')[0]  # 昨日访问
    wz = root.xpath('//ul[@class="space_statistics"]/li[3]/span/text()')[0]  # 总文章
    hz = root.xpath('//ul[@class="space_statistics"]/li[4]/span/text()')[0]  # 总患者
    hz_z = root.xpath('//ul[@class="space_statistics"]/li[5]/span/text()')[0]  # 昨日诊后报到患者
    hz_weixin = root.xpath('//ul[@class="space_statistics"]/li[6]/span/text()')[0]  # 微信诊后报到患者
    hz_weixin = root.xpath('//ul[@class="space_statistics"]/li[7]/span/text()')[0]  # 总诊后报到患者
    hz_tp = root.xpath('//ul[@class="space_statistics"]/li[8]/span/text()')[0]  # 患者投票
    zaixian = root.xpath('//ul[@class="space_statistics"]/li[11]/span/text()')[0]  # 上次在线
    kaitong = root.xpath('//ul[@class="space_statistics"]/li[11]/span/text()')[0]  # 开通时间
    # 检索是否有【电话咨询】标签
    for s in driver.find_elements_by_xpath('//ul[@class="clearfix f16"]/li'):
        if s.text == "电话咨询":
            s.click()
            break
    el = WebDriverWait(driver, 50).until(
        EC.presence_of_element_located((By.XPATH, '//div[@class="zxNeedRead"]/a')))
    el.click()
    root1 = etree.HTML(driver.page_source)
    tp = root1.xpath('//p[@class="p-width lh32 mt5"][1]/span/text()')[0]  # 患者投票数
    fw = root1.xpath('//p[@class="p-width lh32 mt5"][2]/span/text()')[0]  # 在线服务患者数
    aa = root1.xpath('//p[@class="service-name"]/span[1]/text()')[0]  # 图文问诊
    bb = root1.xpath('//p[@class="service-name"]/span[2]/text()')[0]  # 价格
    aa = root1.xpath('//p[@class="service-name"]/span[1]/text()')[1]  # 一问一答
    bb = root1.xpath('//p[@class="service-name"]/span[2]/text()')[1]  # 价格
    aa = root1.xpath('//p[@class="service-name"]/span[1]/text()')[2]  # 电话问诊
    bb = root1.xpath('//p[@class="service-name"]/span[2]/text()')[2]  # 价格
    aa = root1.xpath('//p[@class="service-name"]/span[1]/text()')[3]  # 电话问诊
    bb = root1.xpath('//p[@class="service-name"]/span[2]/text()')[3]  # 价格

def close_driver(driver):
    driver.quit()


if __name__ == '__main__':
    url = "https://qiuweimin.haodf.com"
    driver = init_driver(url)
    for page in range(11):
        get_url(driver)
